<template>
	<view>
		<!-- 轮播图 -->
		<u-swiper :circular="true" height="100vw" :list="swiperImgList" @change="e => current = e.current"
			:autoplay="true">
			<view slot="indicator" class="indicator">
				<view class="indicator__dot" v-for="(item, index) in swiperImgList" :key="index"
					:class="[index === current && 'indicator__dot--active']">
				</view>
			</view>
		</u-swiper>
		<!-- 备注说明 -->
		<iconTxt></iconTxt>
		<!-- 标题价格 -->
		<view class="titleAndPrice">
			<view style="font-size: 20px;">{{title}}</view>
			<view style="font-size: 14px;">{{txt}}</view>
			<view class="price">{{'￥ '+ price +' 元'}}</view>
		</view>
		<!-- 展示弹出层 -->
		<view class="showPopup" @click="changeShowPopup">
			<view>展示弹出层</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<!-- 商品参数 -->
		<view class="goodsParam">
			<view class="param-title">商品参数</view>
			<u--form labelPosition="left" labelWidth="100px" labelAlign="right" v-for="(item,index) in attributeList"
				:key="index">
				<view class="goodsParam-item">
					<u-form-item :label="item.name" borderBottom ref="item1">
						<view class="param-txt">{{item.value}}</view>
					</u-form-item>
				</view>
			</u--form>
		</view>
		<!-- 商品详情 -->
		<!-- <view class="goods-desc" v-html="goodsDesc"></view> -->
		<u-parse :content="goodsDesc"></u-parse>
		<!-- 常见问题 -->
		<view class="problem">
			<view class="divider">
				<u-divider lineColor="#666" textColor="#000" textSize="16" text="常见问题"></u-divider>
			</view>
			<view class="problem-item" v-for="item in issueList" :key="item.id">
				<view class="question">
					<view class="dot"></view>
					{{item.question}}
				</view>
				<view class="answer">
					{{item.answer}}
				</view>
			</view>
			<!-- 相关产品 -->
			<view class="divider">
				<u-divider lineColor="#666" textColor="#000" textSize="16" text="相关产品"></u-divider>
			</view>
		</view>
		<!-- 相关产品列表 -->
		<goodsList :list="goodsList"></goodsList>
		<view class="footer-top"></view>
		<!-- 底部通栏 -->
		<view class="footer">
			<view class="iconAndCart">
				<u-icon v-if="showIcon" size="27" space="10px" name="star" labelPos="button" label="未收藏"
					@click="showIcon = false"></u-icon>
				<u-icon v-else="!showIcon" color="red" size="27" space="10" name="star-fill" labelPos="button"
					label="已收藏" @click="showIcon = true"></u-icon>
			</view>
			<view class="iconAndCart" @click="toCart">
				<u-icon size="27" name="shopping-cart" labelPos="button" label="购物车"></u-icon>
				<view v-if="showTotal" class="cartTotal">{{cartTotal}}</view>
			</view>
			<view class="buy">
				<view class="buyNow">立即购买</view>
				<view class="buyNow addCart" @click="addCart">加入购物车</view>
			</view>
		</view>
		<!-- 弹出层 -->
		<u-popup :closeable="true" :show="showPopup" @close="onClose"
			:customStyle="{width:'100%',padding:'20rpx 20rpx 152rpx 20rpx','box-sizing':'border-box'}">
			<view class="popup-box">
				<view class="imgAndTxt">
					<u--image width="200rpx" height="200rpx" :src="listPicUrl" mode="widthFix"></u--image>
					<view class="txt-box">
						<view>价格：{{'￥ '+ goodsPrice +' 元'}}</view>
						<view>库存：{{goodsNumber}}</view>
					</view>
				</view>
				<view class="count">数量：</view>
				<u-number-box v-model="value"></u-number-box>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		goodsDetailApi,
		goodsRelatedApi,
		cartGoodscountApi,
		cartAddApi
	} from "../request/api.js"
	import request from "../request/request.js"
	export default {
		data() {
			return {
				id: '',
				swiperImgList: [],
				current: 0,
				title: '',
				txt: '',
				price: '',
				attributeList: [],
				goodsDesc: '',
				issueList: [],
				goodsList: [],
				value3: 'play-right',
				cartTotal: '',
				showPopup: false,
				showIcon: true,
				value: 1,
				goodsNumber: '',
				goodsPrice: '',
				listPicUrl: '',
				showTotal: false,
				productId: ''
			}
		},
		onLoad(options) {
			// console.log(options.id);
			this.id = options.id
		},
		onShow() {
			request({
				url: goodsDetailApi,
				data: {
					id: this.id
				},
				successCb: res => {
					console.log(res);
					const {
						gallery,
						info,
						attribute,
						issue,
						productList
					} = res
					this.swiperImgList = gallery.map(item => item.img_url)
					this.title = info.name
					this.txt = info.goods_brief
					this.price = info.retail_price.toFixed(2)
					this.attributeList = attribute
					this.goodsDesc = info.goods_desc
					this.issueList = issue
					this.goodsNumber = productList[0].goods_number
					this.goodsPrice = productList[0].retail_price
					this.productId = productList[0].id
					this.listPicUrl = info.list_pic_url
					// console.log(this.issueList);
				}
			});
			request({
				url: goodsRelatedApi,
				data: {
					id: this.id
				},
				successCb: res => {
					this.goodsList = res.goodsList
					// console.log(this.goodsList);
				}
			});
			this.cartCount();
		},
		methods: {
			// 点击购物车跳转
			toCart() {
				const token = uni.getStorageSync('token')
				if (!token) {
					uni.$u.toast('即将跳转到登录页面')
					setTimeout(() => {
						uni.switchTab({
							url: '../user/user'
						})
					}, 1000)
					return
				}
				uni.switchTab({
					url: "../cart/cart"
				})
			},
			// 点击显示弹出层
			changeShowPopup() {
				this.showPopup = true
			},
			onClose() {
				this.showPopup = false
			},
			// 购物车数量
			cartCount() {
				request({
					url: cartGoodscountApi,
					successCb: res => {
						// console.log(res);
						const token = uni.getStorageSync('token')
						if (token) {
							this.showTotal = true
							this.cartTotal = res.cartTotal.goodsCount
						}
					}
				})
			},
			// 加入购物车
			addCart() {
				if (!this.showPopup) {
					this.showPopup = true
					return
				} else {
					// console.log(111);
					request({
						url: cartAddApi,
						method: "POST",
						data: {
							goodsId: this.id,
							productId: this.productId,
							number: this.value
						},
						successCb: res => {
							// console.log(res);
							uni.$u.toast('添加成功')
							setTimeout(() => {
								this.showPopup = false
								this.cartCount()
							}, 500)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 轮播图
	.indicator {
		@include flex(row);
		justify-content: center;

		&__dot {
			height: 6px;
			width: 6px;
			border-radius: 100px;
			background-color: rgba(255, 255, 255, 0.35);
			margin: 0 5px;
			transition: background-color 0.3s;

			&--active {
				background-color: #8b0000;
			}
		}
	}

	// 标题价格
	.titleAndPrice {
		background-color: #fff;
		text-align: center;
		line-height: 90rpx;
		padding: 30rpx 0;
		border-bottom: 1px solid #ccc;

		.price {
			color: #8b0000;
		}
	}

	// 展示弹出层
	.showPopup {
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		background-color: #fff;
		font-size: 28rpx;
		margin-bottom: 40rpx;
	}

	// 商品参数
	.goodsParam {
		background-color: #fff;
		padding: 30rpx 15rpx;

		.param-title {
			padding: 15rpx;
		}

		.goodsParam-item {
			background-color: #efefef;
			margin-bottom: 10rpx;
			border-radius: 10rpx;

			.param-txt {
				display: inline-block;
				width: 250px;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 1;
				overflow: hidden;
			}
		}

		/deep/ .u-form-item__body__left__content__label {
			color: #999;
		}

		/deep/ .u-form-item__body {
			padding: 10rpx;
		}
	}

	// 商品详情
	.goods-desc {
		width: 100vw;
	}

	// 常见问题
	.problem {
		background-color: #fff;
		padding: 20rpx 40rpx;

		.divider {
			width: 50%;
			text-align: center;
			margin: 0 auto;
		}

		.question {
			padding: 20rpx 0;
			position: relative;

			.dot {
				position: absolute;
				width: 5px;
				height: 5px;
				background-color: #8b0000;
				border-radius: 50%;
				left: -12px;
				top: 18px;
			}
		}

		.answer {
			font-size: 28rpx;
		}
	}

	.footer-top {
		padding-bottom: 75px;
	}

	// 底部通栏
	.footer {
		width: 100%;
		position: fixed;
		z-index: 99999;
		bottom: 0;
		left: 0;
		padding: 20rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		text-align: center;
		align-items: center;
		border-top: 1px solid #999;

		.iconAndCart {
			position: relative;

			.cartTotal {
				position: absolute;
				right: 0;
				top: 0;
				width: 20px;
				height: 17px;
				font-size: 14px;
				color: #fff;
				text-align: center;
				line-height: 17px;
				border-radius: 50%;
				background-color: red;
			}
		}


		/deep/ .u-icon__label {
			padding-top: 5px;
		}

		.buy {
			display: flex;

			.buyNow {
				width: 220rpx;
				color: #fff;
				padding: 20rpx 0;
				background-color: #f40;
				border-radius: 30px 0 0 30px;
			}

			.addCart {
				border-radius: 0 30px 30px 0;
				background-color: #ff8d17;
			}
		}
	}

	// 弹出层样式
	.popup-box {
		.imgAndTxt {
			display: flex;
		}

		.txt-box {
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			margin-left: 30rpx;
			font-size: 28rpx;
		}

		.count {
			margin: 30rpx 0;
			font-size: 40rpx;
		}
	}
</style>
